<template>
  <div :id="eId" style="width: 100%; height: 100%"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'LineEchart',
  props: {
    dataSource: {
      type: Object,
      default () {
        return {
          data: [30, 90, 70, 150, 268, 102, 50],
          xAxis: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        }
      }
    },
    color: {
      type: Array,
      default () {
        return ['#000000', 'rgba(0,0,0,.8)', 'rgba(0,0,0,.3)']
      }
    },
    echatsType: {
      type: String,
      default () {
        return 'line'
      }
    },
    eId: {
      type: String,
      default () {
        return 'main'
      }
    }
  },
  data () {
    return {
      myChart: null
    }
  },
  computed: {},
  mounted () {
    this.init()
  },
  methods: {
    init () {
      this.myChart = echarts.init(document.getElementById(this.eId))
      var option = {
        title: {},
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          left: 5,
          top: 5,
          right: 5,
          bottom: 20
        },
        xAxis: {
          show: false,
          type: 'category',
          boundaryGap: false,
          data: this.dataSource.xAxis
        },
        yAxis: {
          show: false,
          type: 'value',
          axisLabel: {
            formatter: '{value}'
          }
        },
        series: [
          {
            type: this.echatsType,
            data: this.dataSource.data,
            showSymbol: false,
            itemStyle: {
              normal: {
                lineStyle: {
                  color: this.color[0],
                  width: 2
                }
              }
            },
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: this.color[1]
                },
                {
                  offset: 1,
                  color: this.color[2]
                }], false)
              }
            }
          }
        ]
      }

      option && this.myChart.setOption(option)
      let that = this
      setTimeout(() => {
        that.myChart.resize()
      }, 200)
      window.addEventListener('resize', () => {
        setTimeout(() => {
          that.myChart.resize()
        }, 200)
      })
    }
  },
  watch: {
    dataSource () {
      this.init();
    }
  }
}
</script>
